<template>
    <div>
        <H1>文档添加</H1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>标题</td>
                    <td><input type="text" v-model="info.title"></td>
                </tr>
                 <tr>
                    <td>栏目分类</td>
                    <td>
                         <select v-model="info.lid">
            <option value="">请选择</option>
            <option :value="item.pid" v-for="item in level" :key="item.pid">{{item.pname}}</option>  
        </select>
                    </td>
                </tr>
                 <tr>
                    <td>副标题</td>
                    <td><input type="text" v-model="info.otitle"></td>
                </tr>
                 <tr>
                    <td>文档标签</td>
                    <td><input type="text" v-model="info.tips"></td>
                </tr>
                 <tr>
                    <td>图片</td>
                    <td><input type="file" @change="img">
                        <img :src="info.img" width="50px" height="50px">
                    </td>
                </tr>
                 <tr>
                    <td>价格</td>
                    <td><input type="text" v-model="info.money"></td>
                </tr>
                 <tr>
                    <td>颜色</td>
                    <td><input type="radio" name="aa" v-model="info.color">银色
                        <input type="radio" name="aa" v-model="info.color">绿色
                        <input type="radio" name="aa" v-model="info.color">黑色
                        <input type="radio" name="aa" v-model="info.color">灰色
                    </td>
                </tr>
                 <tr>
                    <td>发行日期</td>
                    <td><input type="date" v-model="info.time"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>
        {{info}}
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref} from 'vue'
import { onMounted } from 'vue';

let info:any=ref({
    "id": 0,
  "title": "",
  "otitle": "",
  "tips": "",
  "img": "",
  "money": "",
  "color": "",
  "time": "",
  "lid": ""
})

let level:any=ref([]);
onMounted(()=>{
    getlevel();
})

const add=()=>{
    if(info.value.title==""){
        alert("标题不能为空");
        return;
    }
     if(info.value.otitle==""){
        alert("副标题不能为空");
        return;
    }
    axios({
        url:'https://localhost:7022/api/Phone/AddPhone',
        method:'post',
        data:info.value
    })
    .then((res)=>{
        console.log(res)

        if(res.data==-1){
            alert("账号已存在");
            return;
        }
        if(res.data>0){
            alert("添加成功");
            location.href='/Showall'
        }
        else
        {
            alert("添加失败")
        }
    })
    .catch((err)=>{
        console.log(err)
    })
}

const getlevel=()=>{
    axios({
        url:'https://localhost:7022/api/Phone/PhoneLevel',
        method:'get',
    })
    .then((res)=>{
        console.log(res);
        level.value=res.data
    })
    .catch((err)=>{
        console.log(err);
    })
}
const img=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append('file',obj);
    axios({
        url:'https://localhost:7022/api/Img/Img',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="不是图片"){
            alert("您上传的不是图片");
            return;
        }
        if(res.data=="图片大于2M"){
            alert("上传的图片不能大于2M");
            return;
        }
        info.value.img=res.data
    })
}
</script>

<style scoped>

</style>